import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import ScheduleForClass from '@/views/ScheduleForClass.vue'
import ScheduleForTeacher from '@/views/ScheduleForTeacher.vue'
import User from '@/views/User.vue'
import DseClass from '@/views/DseClass.vue'
import Course from '@/views/Course.vue'
import ScheduleTemplate from '@/views/ScheduleTemplate.vue'
import My from '@/views/My.vue'
import Logout from '@/views/Logout.vue'
import UserDetail from '@/views/UserDetail.vue'
import DseClassDetail from '@/views/DseClassDetail.vue'
import CourseDetail from '@/views/CourseDetail.vue'
import MoveSchedule from '@/views/MoveSchedule.vue'
import TableExport from '@/views/TableExport.vue'
import SumExport from '@/views/SumExport.vue'
import DiffExport from '@/views/DiffExport.vue'
import Page401 from '@/views/401.vue'

import { useUserStore } from '@/stores/user'
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: { hideMenu: true, noAuth: true }
    }, {
      path: '/',
      redirect: () => {
        const userStore = useUserStore()

        if (userStore.isAdm) {
          return '/scheduleforclass'
        } else {
          return '/scheduleforteacher'
        }
      }
    }, {
      path: '/scheduleforclass',
      name: 'ScheduleForClass',
      component: ScheduleForClass,
      meta: {}
    }, {
      path: '/scheduleforteacher',
      name: 'ScheduleForTeacher',
      component: ScheduleForTeacher,
      meta: {}
    }, {
      path: '/user',
      name: 'User',
      component: User,
      meta: { keepAlive: true }
    }, {
      path: '/userdetail',
      name: 'UserDetail',
      component: UserDetail,
    }, {
      path: '/dseclass',
      name: 'DseClass',
      component: DseClass,
      meta: { keepAlive: true }
    }, {
      path: '/dseclassdetail',
      name: 'DseClassDetail',
      component: DseClassDetail,
    }, {
      path: '/course',
      name: 'Course',
      component: Course,
      meta: { meta: { keepAlive: true } }
    }, {
      path: '/coursedetail',
      name: 'CourseDetail',
      component: CourseDetail,
    }, {
      path: '/scheduletemplate',
      name: 'ScheduleTemplate',
      component: ScheduleTemplate,
      meta: {}
    }, {
      path: '/my',
      name: 'My',
      component: My,
      meta: {}
    }, {
      path: '/moveSchedule',
      name: 'moveSchedule',
      component: MoveSchedule,
      meta: {}
    }, {
      path: '/tableExport',
      name: 'tableExport',
      component: TableExport,
      meta: {}
    }, {
      path: '/sumExport',
      name: 'sumExport',
      component: SumExport,
      meta: {}
    }, {
      path: '/diffexport',
      name: 'diffExport',
      component: DiffExport,
      meta: {}
    }, {
      path: '/logout',
      name: 'Logout',
      component: Logout,
      meta: { hideMenu: true, noAuth: true }
    }, {
      path: '/401',
      name: '401',
      component: Page401,
      meta: { noAuth: true }
    },
  ],
})
router.beforeEach(async (to, from, next) => {
  if (from.path === '/' && to.path !== '/' && window.location.hash) {
    next()
    return
  }
  const authRequired = !to.meta.noAuth
  const token = localStorage.getItem('token')
  if (authRequired && !token) {
    return next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
  }
  if (to.path === '/login' && token) {
    return next('/')
  }
  next()
})

export default router
